package com.sky.controller.admin;


import com.sky.result.Result;
import io.swagger.annotations.Api;
import org.apache.ibatis.executor.ResultExtractor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RequestMapping("/admin/common")
@RestController
@Api(tags = "上传接口")
public class CommonController {
    @Value("${reggie.path}")
    private String path;//上传路径

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();//获取原始的文件名
        String suffix = originalFilename.substring(originalFilename.indexOf('.'));//后缀
        String fileName = UUID.randomUUID().toString();//防止文件名相同导致覆盖
        File dir = new File(path);
        //判断目录是否存在，不存在则创建
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String imgPathName = fileName + suffix;
        file.transferTo(new File(path + fileName + suffix));//转存到指定位置
        return Result.success(imgPathName);
    }


}
